package com.buka.common;

import com.alibaba.fastjson.JSON;

public class ResultJson<T> {

    private int code;//响应编码
    private String msg;//提示信息
    private T data;    //数据  查询用户信息、用户列表

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    //成功时响应，只返回成功提示
    public static String success(){
        ResultJson<Object> objectResultJson = new ResultJson<>();
        objectResultJson.code = 1;//1表示成功
        objectResultJson.msg = "执行成功";
        return JSON.toJSONString(objectResultJson);
    }

    //成功时响应,包含数据
    public static <T> String success(T data){
        ResultJson<Object> objectResultJson = new ResultJson<>();
        objectResultJson.code = 1;//1表示成功
        objectResultJson.msg = "执行成功";
        objectResultJson.data = data;
        return JSON.toJSONString(objectResultJson);
    }


    //失败时响应，只返回失败提示
    public static String error(){
        ResultJson<Object> objectResultJson = new ResultJson<>();
        objectResultJson.code = -1;// -1 表示失败
        objectResultJson.msg = "执行失败";
        return JSON.toJSONString(objectResultJson);
    }

}
